###############################################################
###############################################################
#### Stefan Müller, Samuel Brazys, and Alexander Dukalskis
#### Replication Scripts for: 
#### Discourse Wars and 'Mask Diplomacy': China's Global Image Management in Times of Crisis 
#### Political Research Exchange, 2024
#### Link to paper: https://doi.org/10.1080/2474736X.2024.2309178
###############################################################
###############################################################

## Note: check the 000_README.pdf file on Harvard Dataverse for 
## the full replication instructions and information on all code scripts.
## Link to Dataverse repository: https://doi.org/10.7910/DVN/KRXMXJ
## Please contact the authors if you have any questions or suggestions. 
## Note: due to copyright restrictions some of the files cannot be shared publicly.
## However, we provide all replication scripts and intermediate objects to reproduce
## the plots and tables included in the paper and Supporting Information.

## This file visualises the results from the "peak deaths" analysis.
## The models are estimated in 05_prepare_did_data_and_peak_analysis.do.

# load packages
library(readr)
library(dplyr)
library(ggplot2)
library(stringr)

# load custom ggplot2 scheme
source("function_theme_base.R")

# load Peak Lags Figure data
dat_raw <- read_csv("Peak_Lags_Figure.csv")

# keep the second row (coefficients for peak)
# third row (se for peak)
# and ninth row (Lag)

dat <- dat_raw[c(2, 3, 9), ]

colnames(dat) <- paste0("lag_", dat[3,])

colnames(dat) 

dat_clean <- dat |> 
    rename(indicator = lag_Lag) |> 
    mutate(indicator = ifelse(is.na(indicator), "se", indicator)) |> 
    filter(indicator != "Lag")

dat_long_coef <- dat_clean |> 
    filter(indicator == "peak") |> 
    gather(lag_lead, coef)

dat_long_se <- dat_clean |> 
    filter(indicator == "se") |> 
    gather(lag_lead, se)


dat_long_all <- left_join(dat_long_coef, dat_long_se,
                          by = "lag_lead") |> 
    filter(lag_lead != "indicator") |> 
    mutate(lag_lead = str_remove_all(lag_lead, "lag_")) |> 
    mutate(se = as.numeric(se),
           coef = as.numeric(coef)) |> 
    mutate(lag_lead = factor(lag_lead, levels = c(seq(-9, 9, 1))))

dat_long_all <- dat_long_all %>% 
    mutate(peak_dummy = ifelse(lag_lead == "0", TRUE, FALSE))

# Figure A03
ggplot(data = dat_long_all, aes(x = lag_lead, 
                       y = coef,
                       colour = peak_dummy,
                       shape = peak_dummy)) +
    geom_hline(aes(yintercept = 0),
               linetype = "dashed", colour = "red") +
    geom_point(size = 4) +
    scale_y_continuous(breaks = c(seq(-1, 0.5, 0.2))) +
    geom_linerange(aes(ymin = coef - 1.96 * se,
                       ymax = coef + 1.96 * se),
                   size = 0.5) +
    geom_linerange(aes(ymin = coef - 1.645 * se,
                       ymax = coef + 1.645 * se),
                   size = 1.3) +
    scale_shape_manual(values = c(1, 16)) +
    scale_colour_manual(values = c("grey60", "darkred")) +
    labs(x = "Window Around Peak of Weekly Deaths in 2020",
         y = "China Media Tone") +
    theme(legend.position = "none")
ggsave("fig_a03.pdf", 
       width = 9, height = 5)
ggsave("fig_a03.png", 
       width = 9, height = 5, dpi = 300)

